/*
@The pop component in javascript
@Author by suqirrel.terry 0.8
@If you want close pop-Window,you only need add the code "pop.close()" in your pop-content's js.
@Attention: the iframe mode not support "auto" height;
*/
function Pop(){

}
Pop.prototype={
	callParam: null,
	isIe: (document.all)?true:false,
	type: 1,
	content: "",
	event: null,
	title: "",
	left: -1,
	top: -1,
	width: 500,
	height: "auto",
	background: false,
	ifdrag: true,
	mode: "iframe",
	onSuccess: function(){},
//set the select tag hidden
setSelectState: function(state)
{
	 var objl=document.getElementsByTagName('select');
	 for(var i=0;i<objl.length;i++)
	 {
	 objl[i].style.visibility=state;
	 }
},
mousePosition: function(ev)
	 {
	 if(ev.pageX || ev.pageY)
	 {
	 return {x:ev.pageX, y:ev.pageY};
	 }
	 return {
	 x:(ev.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)),y:(ev.clientY + (document.documentElement.scrollTop || document.body.scrollTop))
	 };
 },
//pop main body
showMessageBox: function(wTitle,content,pos,leftPos,topPos,wWidth,wHeight,background)
{
	 var left;
	 var top;
	 var styleStr;
	 this.closeWindow();
	 //var bWidth=parseInt(document.body.scrollWidth);
	 //var bHeight=parseInt(document.body.offsetHeight);

		 if(this.isIe){
		 this.setSelectState('hidden');}
		 var back=document.createElement("div");
		 back.id="back";
		 var sWidth=parseInt(document.documentElement.scrollWidth);
 		 var sHeight=parseInt(document.documentElement.scrollHeight);
		 if(background==true){
		 	styleStr="top:0px;left:0px;position:absolute;background:#000000;width:"+sWidth+"px;height:"+sHeight+"px;";
		 	styleStr+=(this.isIe)?"filter:alpha(opacity=50);":"opacity:0.5;";
		 	back.style.cssText=styleStr;
		 	document.body.appendChild(back);
	 	 	this.showBackground(back,70);
		 }
		 else{
		 	styleStr="top:0px;left:0px;position:absolute;background:#FFFFFF;width:"+sWidth+"px;height:"+sHeight+"px;";
		 	styleStr+=(this.isIe)?"filter:alpha(opacity=0);":"opacity:0;";
		 	back.style.cssText=styleStr;
		 	document.body.appendChild(back)
		 }
	if(leftPos!=-1)
		left=leftPos;
	else if(pos!=null)
		left=((pos.x+20+wWidth)>document.body.clientWidth)?(document.body.clientWidth-wWidth-50):(pos.x+20);
	else
		left=(document.documentElement.clientWidth-wWidth)/2 + (document.documentElement.scrollLeft || document.body.scrollLeft);

	if(topPos!=-1)
		top=topPos;
	else if(pos!=null)
		top=pos.y+30;
	else
   		//if top is not assigned,the messagewindow will be popped up in the middle of browser's window;
		top= (document.documentElement.clientHeight-wHeight)/2 + (document.documentElement.scrollTop || document.body.scrollTop);

	if(left<0)
		left=0;
	if(top<0)
		top=0;

	 var diagBox=document.createElement("div");
	 diagBox.id="diagBox";
	 diagBox.className="diagBox";
	 styleStr="border:1px solid #bfbfbf;background:#ffffff;z-index:999999;left:"+left+"px;top:"+top+"px;position:absolute;";
	 diagBox.style.cssText=styleStr;

	 var mesW=document.createElement("div");
	 mesW.id="mesWindow";
	 mesW.className="mesWindow";
	 if(wHeight=="auto")
	 	styleStr="background:#ffffff;padding-bottom:30px;z-index:100;width:"+wWidth+"px;height:auto;clear:both;overflow:auto;";
	 else
	 	styleStr="background:#ffffff;padding-bottom:30px;z-index:100;width:"+wWidth+"px;height:"+wHeight+"px;";
	 mesW.style.cssText=styleStr;
	 mesW.innerHTML="";
	 mesW.innerHTML+="<div class='mesWindowTop' style='border-bottom:#fff 1px solid;height:20px;cursor:move;padding:1px;font-weight:bold;text-align:left;font-size:14px;background-color:#fff;'><table width='100%' height='100%'><tr><td id='mesWindowTitle'  style='padding:5px;'>"+wTitle+"</td><td align='right' width='20px' ><input type='image' src='http://www.moko.cc/images/pop/close.gif' onclick='pop.closeWindow();clearTimeout(timeout);' title='关闭' class='close' style='cursor:pointer;'/></td></tr></table></div>";
	 mesW.innerHTML+="<div class='mesWindowContent' id='mesWindowContent' style='height:auto;padding:10px;'></div>";

	 diagBox.appendChild(mesW);
	 document.body.appendChild(diagBox);


},
showShadow:function(width,height){
	 var shadow=document.createElement("div");
	 shadow.id="shadow";
	 var styleStr="background:#000;z-index:-20;"+"position:absolute;left:5px;top:5px;width:"+(width+5)+"px;height:"+(height+5)+"px;";
	 styleStr+=(this.isIe)?"filter:alpha(opacity=50);":"opacity:0.5;";
	 shadow.style.cssText=styleStr;

	 document.getElementById('diagBox').appendChild(shadow);
},
showBackground: function(obj,endInt)
{
	 if(this.isIe)
	{
	 obj.filters.alpha.opacity+=1;
	 if(obj.filters.alpha.opacity<endInt)
	 {
	 setTimeout(function(){pop.showBackground(obj,endInt)},10);
	 }
	 }else{
	 var al=parseFloat(obj.style.opacity);al+=0.01;
	 obj.style.opacity=al;
	 if(al<(endInt/100))
	 {setTimeout(function(){pop.showBackground(obj,endInt)},10);}
	 }
},
//close the messwindow
closeWindow: function()
{
	 if(document.getElementById('back')!=null)
	 {
	 document.getElementById('back').parentNode.removeChild(document.getElementById('back'));
	 }
	 if(document.getElementById('diagBox')!=null)
	 {
		document.getElementById('diagBox').parentNode.removeChild(document.getElementById('diagBox'));
	 }
	 if(this.isIe){
	 this.setSelectState('');
	}
},
close: function(param)
{
	this.callParam=param;
	this.closeWindow();
},
//this function make dragging available
drag: function(o)
{
	m=document.getElementById("diagBox");
	var drag=false;
		o.onmousedown=function(a){
		if(!a)a=window.event;
		var x=a.pageX || (a.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
		var y=a.pageY || (a.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
		x=x-parseInt(m.style.left);
		y=y-parseInt(m.style.top);
		if(o.setCapture)
			o.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		drag=true;

		 o.onmousemove=function(a){
		 if(drag){
			if(!a)a=window.event;
			if(!a.pageX)a.pageX=(a.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
			if(!a.pageY)a.pageY=(a.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
			var tx=a.pageX-x,ty=a.pageY-y;
			m.style.left=tx+"px";
			m.style.top=ty+"px";
			}

		};
		//o.addEventListener("mousemove",)
		m.onmouseup=function(){
			if(o.releaseCapture)
				o.releaseCapture();
			else if(window.captureEvents)
				window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			o.onmousemove=null;
			o.onmouseup=null;
			drag=false;

		};

	};
},

popWindow: function(ev,title,left,top,width,height,src,background,ifdrag)
{
	 if(ev!=null)
	 	var objPos = this.mousePosition(ev);
	// messContent = "<iframe id='popiframe' name='popiframe' align='no' style='border:none' scrolling='no' frameborder='0' width='"+width+"' height='"+height+"'></iframe>";

	 this.showMessageBox(title,src,objPos,left,top,width,height,background);
	 //this.showShadow();
	 if(ifdrag){
		this.drag(document.getElementById('mesWindowTitle'));
	}
},




getAjax: function(){
	try{
		return new ActiveXObject('Msxml2.XMLHTTP');
	}catch(e){
		try{
			return new ActiveXObject('Microsoft.XMLHTTP');
		}catch(e){
			try{
				return new XMLHttpRequest();
			}catch(e){}
		}
	}
},

init: function(popObj){
	this.event=popObj.event?popObj.event:null;
	this.title=popObj.title?popObj.title:"";
	this.left=popObj.left?popObj.left:-1;
	this.top=popObj.top?popObj.top:-1;
	this.width=popObj.width?popObj.width:500;
	this.height=popObj.height?popObj.height:"auto";
	this.background=(popObj.background==undefined)?false:popObj.background;
	this.ifdrag=(popObj.ifdrag==undefined)?true:popObj.ifdrag;
	this.mode = popObj.mode?popObj.mode:"iframe";
	this.onSuccess = popObj.onSuccess?popObj.onSuccess:function(){},
	this.content="";
	this.show();


	switch(popObj.type){
		case 1:
				if(popObj.mode!="div"){
					document.getElementById('mesWindowContent').innerHTML= "<iframe id='popiframe' src="+popObj.url+" align='no' style='border:none' scrolling='no' frameborder='0' width='"+(popObj.width-20)+"' height='"+(popObj.height-53)+"'></iframe>";
					/*判断ie6或ie7的版本 by yuanqian 2008-10-25*/
					var browser=navigator.appName
					var b_version=navigator.appVersion
					var version=b_version.split(";");
					var trim_Version=version[1].replace(/[ ]/g,"");
					var ieType=0;
					if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE6.0")
					{
						ieType=6;
					}
					else if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE7.0"){
						ieType=7;
					}
					if(ieType==6){
						window.frames['popiframe'].location.reload();
					}
					break;
				}
				var response=null;
				var xmlHttp = this.getAjax();
				xmlHttp.onreadystatechange=function(){
					if(xmlHttp.readyState==4){
						if(xmlHttp.status==200){
							document.getElementById('mesWindowContent').innerHTML=xmlHttp.responseText;
							pop.onSuccess();
						}
						else{
							alert(xmlHttp.status);
						}
					}
					else if (xmlHttp.readyState==3)
					{
						//show the waiting... information
					}

				}
				xmlHttp.open("get",popObj.url);
				xmlHttp.send(null);
				break;
		case 2:
				document.getElementById('mesWindowContent').innerHTML=popObj.src;
				break;
		case 3:
				document.getElementById('mesWindowContent').innerHTML="<table width='100%' border='0' cellspacing='2' cellpadding='0'><tr><td>"+popObj.src+"</td></tr><tr><td><div align='center' style='padding-top:10px; padding-bottom:20px; '><table width='100%' border='0' align='center'  cellspacing='7' cellpadding='0'> <tr><td><div align='right'><input class='buttonM_1 font14 pink weight700' type='button' value='确 定' name='imageField2' id='imageField2' onfocus='this.blur()' onclick='"+popObj.callback+"("+popObj.param+")"+"' /></div></td><td><div align='left'><input class='buttonM_1 font14' type='button' name='imageField' id='imageField' value='取 消' onfocus='this.blur()' onclick='pop.closeWindow()' /></div></td></tr></table></div></td></tr></table>";
				this.callback = popObj.callback;
				break;
		default:
				alert("the type must be [1,2,3]");
				break;

}
var sWidth = this.width;
var sHeight = parseInt(document.getElementById("diagBox").offsetHeight);
this.showShadow(sWidth,sHeight);

},

show: function(){
	this.popWindow(this.event,this.title,this.left,this.top,this.width,this.height,this.content,this.background,this.ifdrag);
}
}

var pop = new Pop();